package servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;




import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONObject;
import dao.AdminDao;
import dbutil.DBHelper;
import entity.Admin;
import entity.AdminPrivs;

public class EditAdminServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] addPrivArr;
	private String[] delPrivArr;
	private Map<String, String> privNameAddUser = new HashMap<String, String>();
	private Map<String, Date> privNameDate = new HashMap<String, Date>();
	JSONObject jsonObject = new JSONObject();
	boolean isSuccess = false;
	Connection conn;
	
	public EditAdminServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		AdminDao adminDao = null;
		AdminPrivs adminPrivs = null;
		Admin admin = null;
		
		try {
			conn = DBHelper.getConnection();
			conn.setAutoCommit(false);// 关闭事务自动提交
			adminDao = new AdminDao();
			
			// 管理员
			admin = new Admin();
			admin.setAdminName(request.getParameter("adminName"));
			admin.setPassword(request.getParameter("password"));
			admin.setCnName(request.getParameter("cnName"));
			admin.setCreateDate(null);
			admin.setDeptName(request.getParameter("deptName"));
			admin.setEmail(request.getParameter("email"));
			admin.setIsSuper(request.getParameter("isSuper"));
			// admin.setStatus(request.getParameter("status"));
			admin.setPhone(request.getParameter("phone"));
			// 管理员权限
			adminPrivs = new AdminPrivs();
			adminPrivs.setAdminName(request.getParameter("adminName"));
			setAdminPrivsPro(request);
			adminPrivs.setPrivNameAddUser(privNameAddUser);
			adminPrivs.setPrivNameDate(privNameDate);
			DBHelper.getConnection().setAutoCommit(false);
			
			// 每一个dao出错即rollback
			isSuccess = adminDao.editAdmin(admin);
			if(addPrivArr != null && addPrivArr.length > 0) {// 添加权限
				isSuccess = adminDao.addAdminPrivs(adminPrivs);
			}
			
			if(delPrivArr !=null && delPrivArr.length > 0) {// 删除权限
				isSuccess = adminDao.delAdminPrivs(delPrivArr, admin.getAdminName());
			}
			judgeConn();
		} catch (SQLException e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "EditAdminServlet Exception");
		} finally {
			out.print(jsonObject.toString());
			out.close();
		}
	}

	// 处理权限字符串
	private String[] splitPrivs(String privs) {
		return privs.split("、");
	};
	// 清空存储权限的变量
	private void cleanPrivsArrMap() {
		privNameAddUser.clear();
		privNameDate.clear();
		delPrivArr = null;
		addPrivArr = null;
	}
	// 处理权限-添加人-日期
	private void setAdminPrivsPro(HttpServletRequest request) {
		cleanPrivsArrMap();
		Admin admin = (Admin) request.getSession().getAttribute("admin");// 当前操作的管理员
		String addUser = admin.getAdminName();
		String delPrivs = request.getParameter("delPrivs");
		String addPrivs = request.getParameter("addPrivs");
		
		if(!StringUtils.isEmpty(addPrivs)) {
			addPrivArr = splitPrivs(addPrivs);
			if(addPrivArr.length > 0) {
				for(String priv : addPrivArr) {
					privNameAddUser.put(priv, addUser);
					privNameDate.put(priv, new Date());
				}
			}
		} else {
			addPrivArr = null;
		}
		if(!StringUtils.isEmpty(delPrivs)) {
			delPrivArr = splitPrivs(delPrivs);
		} else {
			delPrivArr = null;
		}
	}
	// 返回值判断
	private void judgeConn() throws SQLException {
		if(isSuccess) {
			jsonObject.put("retCode", "1");
			conn.commit();
		} else {
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", "EditAdminServlet Exception");
			conn.rollback();
		}
		conn.setAutoCommit(true);
	}
	
	public void init() throws ServletException {
	}

}
